package cn.hl.hm.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.entity.DriverSystemInfo;
import cn.hl.hm.entity.SystemInfo;
import cn.hl.hm.entity.User;
import cn.hl.hm.service.DriverSystemInfoService;
import cn.hl.hm.service.SystemInfoService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

@Controller
@RequestMapping("/systemInfoController")
public class SystemInfoController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private SystemInfoService systemInfoService;
	
	@Autowired 
	private DriverSystemInfoService driverSystemInfoService;
	
	/**
	 * 添加新的系统消息
	 * @param systemInfo
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/addSystemInfo", method = RequestMethod.POST)
	public String addSystemInfo(SystemInfo systemInfo,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("添加新的系统消息---systemInfoController/addSystemInfo");
		request.setCharacterEncoding("UTF-8");
		
		try{
			systemInfo.setCreateTime(new Date());
			int i = systemInfoService.insertSelective(systemInfo);
			if(i>0){
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
		}catch(Exception e){
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		return null;
	}
	
	
	/**
	 * 添加司机阅读消息记录
	 * @param driverSystemInfo
	 * @param driverId
	 * @param systemInfoId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/addDriverSystemInfo", method = RequestMethod.POST)
	public String addDriverSystemInfo(DriverSystemInfo driverSystemInfo,
			@RequestParam Integer driverId,
			@RequestParam Integer systemInfoId,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("添加司机阅读消息记录---systemInfoController/addSystemInfo");
		request.setCharacterEncoding("UTF-8");
		
		try{
			int i = driverSystemInfoService.insertSelective(driverSystemInfo);
			if(i>0){
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
		}catch(Exception e){
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		return null;
	}
	
	
	/**
	 * 添加司机阅读消息记录
	 * @param driverSystemInfo
	 * @param driverId
	 * @param systemInfoId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/addDriverSystemInfoAll", method = RequestMethod.POST)
	public String addDriverSystemInfoAll(
			@RequestParam Integer driverId,
			@RequestParam String systemInfoIds,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("添加司机阅读消息记录---systemInfoController/addSystemInfo");
		request.setCharacterEncoding("UTF-8");
		
		try{
			if(!systemInfoIds.equals("")&&systemInfoIds!=null){
				String[] s = systemInfoIds.split(",");
				for (String string : s) {
					if(string!=null&&!string.equals("")){
					int systemInfoId = Integer.parseInt(string);
					DriverSystemInfo driverSystemInfo=new DriverSystemInfo();
					driverSystemInfo.setDriverId(driverId);
					driverSystemInfo.setSystemInfoId(systemInfoId);
					List<DriverSystemInfo> selectDriverSystemInfo = driverSystemInfoService.selectDriverSystemInfo(driverSystemInfo);
					if(selectDriverSystemInfo.size()==0)
					 driverSystemInfoService.insertSelective(driverSystemInfo);
					}
				}
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.PARAM_ERROR, response);
				return null;
			}
		}catch(Exception e){
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		return null;
	}
	
	/**
	 * 查询司机阅读消息信息
	 * @param driverSystemInfo
	 * @param driverId
	 * @param systemInfoId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectSystemInfo", method = RequestMethod.POST)
	public String selectSystemInfo(SystemInfo SystemInfo,
			@RequestParam Integer driverId,
			@RequestParam Integer pageId,
			@RequestParam Integer pageCount,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("添加司机阅读消息记录---systemInfoController/addSystemInfo");
		request.setCharacterEncoding("UTF-8");
		
		try{
			AppPager handle = AppPager.handle(pageId, pageCount);
			SystemInfo.setPageId(handle.getPageId());
			SystemInfo.setPageCount(handle.getPageCount());
			  List<SystemInfo>  SystemInfoList=systemInfoService.selectSystemInfo(SystemInfo);
			  for (SystemInfo systemInfo2 : SystemInfoList) {
					int systemInfoId=systemInfo2.getId();
					DriverSystemInfo driverSystemInfo=new DriverSystemInfo();
					driverSystemInfo.setDriverId(driverId);
					driverSystemInfo.setSystemInfoId(systemInfoId);
					List<DriverSystemInfo> driverSystemInfoList=driverSystemInfoService.selectDriverSystemInfo(driverSystemInfo);
					if(driverSystemInfoList!=null && driverSystemInfoList.size()>0){
						systemInfo2.setStatus("2");
					}else {
						systemInfo2.setStatus("1");
					}
			}
			  SystemInfo.setPageId(null);
			  SystemInfo.setPageCount(null);
			  int num=systemInfoService.selectSystemInfoNum(SystemInfo);
			  Map map =AppPager.getPageInfo(SystemInfoList, pageCount, num,pageId);
				Result.ok(map, InfoCode.SUCCESS, response);
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		return null;
	}
	
	
	
	/**
	 * 查询司机阅读消息信息
	 * @param driverSystemInfo
	 * @param driverId
	 * @param systemInfoId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectSystemInfoNum", method = RequestMethod.POST)
	public String selectSystemInfoNum(SystemInfo SystemInfo,
			@RequestParam Integer driverId,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("添加司机阅读消息记录---systemInfoController/addSystemInfo");
		request.setCharacterEncoding("UTF-8");
		
		try{
			int count =0;
			  List<SystemInfo>  SystemInfoList=systemInfoService.selectSystemInfo(SystemInfo);
			  for (SystemInfo systemInfo2 : SystemInfoList) {
					int systemInfoId=systemInfo2.getId();
					DriverSystemInfo driverSystemInfo=new DriverSystemInfo();
					driverSystemInfo.setDriverId(driverId);
					driverSystemInfo.setSystemInfoId(systemInfoId);
					List<DriverSystemInfo> driverSystemInfoList=driverSystemInfoService.selectDriverSystemInfo(driverSystemInfo);
					if(driverSystemInfoList!=null && driverSystemInfoList.size()>0){
						systemInfo2.setStatus("2");
					}else {
						systemInfo2.setStatus("1");
						count++;
					}
			}
				Result.ok(count, InfoCode.SUCCESS, response);
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		return null;
	}
	
}

